package com.zx.idc.backend.gui.scheduler.slog;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.backend.gui.common.anno.GlobalJob;
import com.zx.idc.backend.gui.slog.service.SlogAbnormalIpInterfaceService;
import com.zx.idc.common.spring.SpringBeanUtil;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.ds.slog.entity.SlogAbnormalIp;
import com.zx.idc.ds.slog.entity.SlogAbnormalIpStatus;
import com.zx.idc.ds.slog.service.ISlogAbnormalIpService;
import com.zx.idc.ds.slog.service.ISlogAbnormalIpStatusService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * @author rxliuli
 */
@GlobalJob
//TODO 这个job作用未知
public class SlogAbnormalIpJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        final ISlogAbnormalIpService service = SpringBeanUtil.getBean(ISlogAbnormalIpService.class);
        final SlogAbnormalIpInterfaceService reportService = SpringBeanUtil.getBean(SlogAbnormalIpInterfaceService.class);
        final ISlogAbnormalIpStatusService slogAbnormalIpStatusService = SpringBeanUtil.getBean(ISlogAbnormalIpStatusService.class);
        service.selectList(new EntityWrapper<>(
            new SlogAbnormalIp().setReportStatus(ReportStatusConstant.NOT_REPORTED)
        ).setSqlSelect("id")).stream()
            .map(SlogAbnormalIp::getId)
            .filter(id -> slogAbnormalIpStatusService.exists(
                new SlogAbnormalIpStatus()
                    .setReportStatus(ReportStatusConstant.NOT_REPORTED)
                    .setAbnormalIpId(id)
            ))
            .forEach(reportService::insertReport);
    }
}
